package src

import (
	"HoxignTodo/src/utils"
	"github.com/gorilla/mux"
	"net/http"
)

type Handler404 struct{}

func (handler Handler404) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.URL.Path != "/404" {
		http.Redirect(w, r, "/404", http.StatusFound)
	} else {
		w.WriteHeader(404)
		utils.ParseTmpl("404", false).Exc(w, nil)
	}
}

// PathPrefix: /

func Index(w http.ResponseWriter, r *http.Request) {
	data := utils.Data{
		NavActive: "index",
		ReturnBtn: false,
		Word:      utils.JudgeGreeting(),
	}
	if utils.TempData.To == r.URL.Path {
		data.CopyMsg(utils.TempData)
		utils.TempData.Clear()
	}
	utils.ParseTmpl("index", true).Exc(w, data)
}

func Incomplete(w http.ResponseWriter, _ *http.Request) {
	data := utils.Data{
		NavActive: "incomplete",
		ReturnBtn: false,
		TaskList:  utils.QueryTasksByState(utils.Tst2),
		Date:      utils.GetAllDay(),
	}
	utils.ParseTmpl("incomplete", true).Exc(w, data)
}

func Finished(w http.ResponseWriter, _ *http.Request) {
	data := utils.Data{
		NavActive: "finished",
		ReturnBtn: false,
		TaskList:  utils.QueryTasksByState(utils.Tst1),
		Date:      utils.GetAllDay(),
	}
	utils.ParseTmpl("finished", true).Exc(w, data)
}

func Trash(w http.ResponseWriter, _ *http.Request) {
	data := utils.Data{
		NavActive: "trash",
		ReturnBtn: false,
		TaskList:  utils.QueryTasksByState(utils.Tst0),
		Date:      utils.GetAllDay(),
	}
	utils.ParseTmpl("trash", true).Exc(w, data)
}

// PathPrefix: /task

func TaskAdd(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodGet {
		data := utils.Data{
			ReturnBtn: true,
			Date:      utils.Date{Today: utils.GetToday()},
		}
		utils.ParseTmpl("task/add", true).Exc(w, data)
	} else if r.Method == http.MethodPost {
		utils.TempData.From = "/newTask/add"
		utils.TempData.To = "/index"
		newTask := utils.TaskItem{
			Id:    utils.CreateUlid(),
			Date:  r.FormValue("date"),
			Name:  r.FormValue("name"),
			Desc:  r.FormValue("desc"),
			State: utils.Tst2,
		}
		if utils.CheckDuplicateTask(newTask) {
			utils.TempData.Msg = "添加任务失败：已有相同任务待完成！"
			utils.TempData.MsgType = "error"
			http.Redirect(w, r, "/index", http.StatusFound)
			return
		}
		utils.TaskWriter(newTask)
		utils.TempData.Msg = "添加任务成功：" + newTask.Name
		utils.TempData.MsgType = "success"
		http.Redirect(w, r, "/index", http.StatusFound)
	}
}

func TaskCheck(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	taskId := vars["taskId"]
	task := utils.QueryTaskById(taskId)
	if task.Id == "" {
		http.Redirect(w, r, "/404", http.StatusFound)
		return
	}
	data := utils.Data{
		From:      "incomplete",
		ReturnBtn: true,
		TaskList:  []utils.TaskItem{task},
	}
	utils.ParseTmpl("task/check", true).Exc(w, data)
}

func TaskDelete(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	taskId := vars["taskId"]
	task := utils.QueryTaskById(taskId)
	if task.Id == "" {
		http.Redirect(w, r, "/404", http.StatusFound)
		return
	}
	task.State = utils.Tst0
	utils.TaskRewriter(task)
	http.Redirect(w, r, "/index", http.StatusFound)
}

func TaskEdit(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	taskId := vars["taskId"]
	task := utils.QueryTaskById(taskId)
	if task.Id == "" {
		http.Redirect(w, r, "/404", http.StatusFound)
		return
	}
	if r.Method == http.MethodGet {
		data := utils.Data{
			ReturnBtn: true,
			TaskList:  []utils.TaskItem{task},
		}
		utils.ParseTmpl("task/edit", true).Exc(w, data)
	} else if r.Method == http.MethodPost {
		utils.TempData.From = "/newTask/edit"
		utils.TempData.To = "/index"
		newTask := utils.TaskItem{
			Id:    taskId,
			Date:  r.FormValue("date"),
			Name:  r.FormValue("name"),
			Desc:  r.FormValue("desc"),
			State: utils.Tst2,
		}
		if utils.CheckDuplicateTask(newTask) {
			utils.TempData.Msg = "更改任务失败：已有相同任务待完成！"
			utils.TempData.MsgType = "error"
			http.Redirect(w, r, "/index", http.StatusFound)
			return
		}
		utils.TaskRewriter(task)
		utils.TempData.Msg = "更改任务成功：" + newTask.Name
		utils.TempData.MsgType = "success"
		http.Redirect(w, r, "/index", http.StatusFound)
	}
}

func TaskRestore(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	taskId := vars["taskId"]
	task := utils.QueryTaskById(taskId)
	if task.Id == "" {
		http.Redirect(w, r, "/404", http.StatusFound)
		return
	}
	task.State = utils.Tst2
	utils.TaskRewriter(task)
	http.Redirect(w, r, "/index", http.StatusFound)
}

func TaskFinish(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	taskId := vars["taskId"]
	task := utils.QueryTaskById(taskId)
	if task.Id == "" {
		http.Redirect(w, r, "/404", http.StatusFound)
		return
	}
	task.State = utils.Tst1
	utils.TaskRewriter(task)
	http.Redirect(w, r, "/index", http.StatusFound)
}
